package xhwl.hmo.mall.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by admin on 2017/11/9.
 */

public class SuitPagerAdapter extends PagerAdapter {
    //viewpager的adapter
    private List<ImageView> listimg;

    public SuitPagerAdapter(List<ImageView> listimg) {
        super();
        this.listimg = listimg;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public int getCount() {
        return Short.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        position %= listimg.size();
        if (position < 0) {
            position = listimg.size() + position;
        }
        ImageView view = listimg.get(position);

        //如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
        ViewParent vp = view.getParent();
        if (vp != null) {
            ViewGroup parent = (ViewGroup) vp;
            parent.removeView(view);
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//            container.removeView((View) object);//去掉容易出现闪屏
    }

}
